package dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import java.util.ArrayList;
import java.util.List;

import beans.ResultSupplierRightsBean;
import beans.SetSupplierRightsBean;
import beans.UserRightsBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import constants.Constants;
import custom.MyDialog;
import netframe.NetAddress;
import utils.ActivityUtil;

/**
 * Created by Administrator on 2018/5/10.
 */

public class UserRightsSupplierDialog extends MyDialog {
    @BindView(R.id.tv_dialog_user_rights_supplier_name)
    TextView tvName;
    @BindView(R.id.tv_dialog_user_rights_supplier_role)
    TextView tvRole;
    @BindView(R.id.cb_dialog_user_rights_supplier_cabinet_first)
    AppCompatCheckBox cbCabinetFirst;
    @BindView(R.id.cb_dialog_user_rights_supplier_cabinet_second)
    AppCompatCheckBox cbCabinetSecond;
    @BindView(R.id.cb_dialog_user_rights_supplier_cabinet_third)
    AppCompatCheckBox cbCabinetThird;
    @BindView(R.id.cb_dialog_user_rights_supplier_cabinet_fourth)
    AppCompatCheckBox cbCabinetFourth;
    @BindView(R.id.cb_dialog_user_rights_supplier_cabinet_fifth)
    AppCompatCheckBox cbCabinetFifth;
    //    @BindView(R.id.cb_dialog_user_rights_supplier_enable)
//    AppCompatCheckBox cbEnable;
//    @BindView(R.id.cb_dialog_user_rights_supplier_disable)
//    AppCompatCheckBox cbDisable;
    @BindView(R.id.tv_dialog_user_rights_supplier_cancle)
    TextView tvCancle;
    @BindView(R.id.tv_dialog_user_rights_supplier_sure)
    TextView tvSure;

    private UserRightsBean userRightsBean;
    private String[] split;
    private Context mContext;
    private SharedPreferences sharedPreferences;
    //用户改变之后的可打开柜子的集合
    private List<String> changeCabinetList = new ArrayList<>();
    //原先的可打开柜子的集合
    private List<String> currentCabinetList = new ArrayList<>();

    private String enable;
    private boolean frist,second,third,fourth,fifth;
    private ResultSupplierRightsBean resultSupplierRightsBean = new ResultSupplierRightsBean();
    private List<SetSupplierRightsBean> increaseBeanList = new ArrayList<>();
    private List<SetSupplierRightsBean> decreaseBeanList = new ArrayList<>();

    private OnClickListener onClickListener;

    public UserRightsSupplierDialog(@NonNull Context context, @StyleRes int themeResId, UserRightsBean userRightsBean, OnClickListener onClickListener) {
        super(context, themeResId);
        this.userRightsBean = userRightsBean;
        this.mContext = context;
        this.onClickListener = onClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_user_rights_supplier);
        ButterKnife.bind(this);
        tvName.setText(userRightsBean.getName());
        tvRole.setText(userRightsBean.getRole());
//        if (userRightsBean.isEnable()){
//            cbEnable.setChecked(true);
//            enable="True";
//        }else{
//            cbDisable.setChecked(true);
//            enable="False";
//        }

        sharedPreferences = ActivityUtil.getSharedPreferences(mContext);
        String enableCabinetInfoIds = sharedPreferences.getString(Constants.ENABLE_LOGICNUMBER, "");
        split = enableCabinetInfoIds.split(",");
        for (int i = 0; i < split.length; i++) {
            initCabinet(split[i]);
        }
    }

    private void initCabinet(String s) {
        changeCabinetList.add(s);
        currentCabinetList.add(s);
        switch (s) {
            case "1":
                cbCabinetFirst.setChecked(true);
                frist = true;
                break;
            case "2":
                second = true;
                cbCabinetSecond.setChecked(true);
                break;
            case "3":
                third = true;
                cbCabinetThird.setChecked(true);
                break;
            case "4":
                fourth = true;
                cbCabinetFourth.setChecked(true);
                break;
            case "5":
                fifth = true;
                cbCabinetFifth.setChecked(true);
                break;
        }
    }

    @OnClick({R.id.cb_dialog_user_rights_supplier_cabinet_first, R.id.cb_dialog_user_rights_supplier_cabinet_second, R.id.cb_dialog_user_rights_supplier_cabinet_third,
            R.id.cb_dialog_user_rights_supplier_cabinet_fourth, R.id.cb_dialog_user_rights_supplier_cabinet_fifth, R.id.tv_dialog_user_rights_supplier_cancle, R.id.tv_dialog_user_rights_supplier_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cb_dialog_user_rights_supplier_cabinet_first:
                changeCabinet(cbCabinetFirst.isChecked(), "1");
                break;
            case R.id.cb_dialog_user_rights_supplier_cabinet_second:
                changeCabinet(cbCabinetSecond.isChecked(), "2");
                break;
            case R.id.cb_dialog_user_rights_supplier_cabinet_third:
                changeCabinet(cbCabinetThird.isChecked(), "3");
                break;
            case R.id.cb_dialog_user_rights_supplier_cabinet_fourth:
                changeCabinet(cbCabinetFourth.isChecked(), "4");
                break;
            case R.id.cb_dialog_user_rights_supplier_cabinet_fifth:
                changeCabinet(cbCabinetFifth.isChecked(), "5");
                break;
//            case R.id.cb_dialog_user_rights_supplier_enable:
//                enable="True";
//                cbDisable.setChecked(false);
//                break;
//            case R.id.cb_dialog_user_rights_supplier_disable:
//                cbEnable.setChecked(false);
//                enable="False";
//                break;
            case R.id.tv_dialog_user_rights_supplier_cancle:
                dismiss();
                break;
            case R.id.tv_dialog_user_rights_supplier_sure:
                //增加的可打开的柜号
                List<String> increaseList = ActivityUtil.getDifferent(currentCabinetList, changeCabinetList);
                //减少的可打开的柜号
                List<String> decreaseList = ActivityUtil.getDifferent(changeCabinetList, currentCabinetList);


                Log.e("CCC", "onViewClicked: "+increaseList.size());
                Log.e("CCC", "onViewClicked: "+decreaseList.size());
                for (int i = 0; i < increaseList.size(); i++) {
                    SetSupplierRightsBean bean = new SetSupplierRightsBean();
                    bean.setTerminalInfoID(NetAddress.terminalInfoId);
                    bean.setLogicNumber(increaseList.get(i));
                    increaseBeanList.add(bean);
                }

                for (int i = 0; i < decreaseList.size(); i++) {
                    SetSupplierRightsBean bean = new SetSupplierRightsBean();
                    bean.setTerminalInfoID(NetAddress.terminalInfoId);
                    bean.setLogicNumber(decreaseList.get(i));
                    decreaseBeanList.add(bean);
                }

                resultSupplierRightsBean.setAddRights(increaseBeanList);
                resultSupplierRightsBean.setLessRights(decreaseBeanList);
                onClickListener.onChangeClickListener(userRightsBean.getName(), enable, resultSupplierRightsBean, this);
                break;
        }
    }

    public void changeCabinet(boolean isChecked, String cabinetNumber) {
        if (isChecked) {
            changeCabinetList.add(cabinetNumber);
        } else {
            changeCabinetList.remove(cabinetNumber);
        }
    }

    public interface OnClickListener {
        void onChangeClickListener(String name, String enable, ResultSupplierRightsBean bean, UserRightsSupplierDialog dialog);
    }

}
